package com.matrix.yukun.matrix.main_module.wallpaper.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.matrix.yukun.matrix.BaseActivity;
import com.matrix.yukun.matrix.R;
import com.matrix.yukun.matrix.main_module.entity.WallpaperInfo;
import com.matrix.yukun.matrix.main_module.netutils.NetworkUtils;
import com.matrix.yukun.matrix.main_module.utils.SpacesDoubleDecoration;
import com.matrix.yukun.matrix.main_module.utils.SpacesItemDecoration;
import com.matrix.yukun.matrix.main_module.wallpaper.adapter.RVWallpaperComputerAdapter;
import com.matrix.yukun.matrix.main_module.wallpaper.adapter.RVWallpaperMessageAdapter;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

public class ComputerWallpaperListActivity extends BaseActivity {

    private static String mId;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_setting)
    ImageView ivBack;
    @BindView(R.id.rl)
    RelativeLayout rl;
    @BindView(R.id.sw_refresh)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    private String url = "http://service.picasso.adesk.com/v1/wallpaper/category/";
    private int skip;
    private List<WallpaperInfo> mWallpaperInfos = new ArrayList<>();
    private RVWallpaperComputerAdapter mAdapter;

    public static void start(String id, Context context) {
        mId = id;
        Intent intent = new Intent(context, ComputerWallpaperListActivity.class);
        context.startActivity(intent);
    }

    @Override
    public int getLayout() {
        return R.layout.activity_computer_wallpaper_list;
    }

    @Override
    public void initView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        recyclerview.setLayoutManager(gridLayoutManager);
        mAdapter = new RVWallpaperComputerAdapter(R.layout.wallpaper_computer_item_layout, mWallpaperInfos);
        recyclerview.setAdapter(mAdapter);
        recyclerview.addItemDecoration(new SpacesDoubleDecoration(10, 10, 5, 0));
    }

    @Override
    public void initListener() {
        mAdapter.setOnLoadMoreListener(() -> {
            skip = skip + 8;
            initDate();
        }, recyclerview);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                skip = 0;
                mWallpaperInfos.clear();
                mAdapter.setNewData(mWallpaperInfos);
                initDate();
            }
        });

        mAdapter.setOnItemClickListener ((adapter, view, position) -> WallpaperPreviewActivity.start(this, mWallpaperInfos.get(position),false));

        ivBack.setOnClickListener(v -> finish());
    }

    @Override
    public void initDate() {
        NetworkUtils.networkGet(url + mId + "/wallpaper?")
                .addParams("limit", 8 + "")
                .addParams("skip", skip + "")
                .addParams("adult", "false")
                .addParams("first", 1 + "")
                .addParams("order", "hot").build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            String msg = jsonObject.optString("msg");
                            if (msg.equals("success")) {
                                JSONObject res = jsonObject.optJSONObject("res");
                                Gson gson = new Gson();
                                List<WallpaperInfo> vertical = gson.fromJson(res.optJSONArray("wallpaper").toString(), new TypeToken<List<WallpaperInfo>>() {
                                }.getType());
                                if (vertical.size() > 0) {
                                    mWallpaperInfos.addAll(vertical);
                                    mAdapter.setNewData(mWallpaperInfos);
                                }
                                mSwipeRefreshLayout.setRefreshing(false);
                                mAdapter.loadMoreComplete();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
